package com.hz.apiService.model.request;

import com.alibaba.fastjson2.JSON;
import com.hz.apiService.model.enums.ApiResponseCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author LiiiYiAn
 * @version 1.0
 * @createDate 2023-09-25 09:58
 * @projectName SchoolServicePlatformFinal
 * @className ApiResponse
 * @description 前端请求响应的封装类，格式化接口返回数据
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ApiResponse<T> {

    private String code;
    private String message;
    private T data;

    public ApiResponse(String code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * @param data 数据类型
     * @return java.lang.String JSON数据
     * @author LiiiYiAn
     * @createDate 2023/9/25 10:05
     * @description 通用的成功方法，只需传入数据即可
     **/
    public static <T> String ok(T data) {
        ApiResponse<T> res = new ApiResponse<>(ApiResponseCodeEnum.SUCCESS.getCode(), ApiResponseCodeEnum.SUCCESS.getMessage(), data);

        return JSON.toJSONString(res);
    }

    /**
     * @return java.lang.String
     * @author LiiiYiAn
     * @createDate 2023/9/25 10:27
     * @description 通用的资源无权访问方法
     **/
    public static String resForbidden() {
        ApiResponse res = new ApiResponse<>(ApiResponseCodeEnum.RESOURCE_FORBIDDEN.getCode(), ApiResponseCodeEnum.RESOURCE_FORBIDDEN.getMessage());

        return JSON.toJSONString(res);
    }

    /**
     * @return java.lang.String
     * @author LiiiYiAn
     * @createDate 2023/9/25 10:31
     * @description 通用的接口禁止方法
     **/
    public static String intForbidden() {
        ApiResponse res = new ApiResponse<>(ApiResponseCodeEnum.INTERFACE_FORBIDDEN.getCode(), ApiResponseCodeEnum.INTERFACE_FORBIDDEN.getMessage());

        return JSON.toJSONString(res);
    }

    /**
     * @return java.lang.String
     * @author LiiiYiAn
     * @createDate 2023/9/25 10:32
     * @description 通用的资源不存在方法
     **/
    public static String resNotFound() {
        ApiResponse res = new ApiResponse<>(ApiResponseCodeEnum.RESOURCE_NOT_FOUND.getCode(), ApiResponseCodeEnum.RESOURCE_NOT_FOUND.getMessage());

        return JSON.toJSONString(res);
    }

    /**
     * @return java.lang.String
     * @author LiiiYiAn
     * @createDate 2023/9/25 10:32
     * @description 通用的接口错误方法
     **/
    public static String intError() {
        ApiResponse res = new ApiResponse<>(ApiResponseCodeEnum.INTERFACE_ERROR.getCode(), ApiResponseCodeEnum.INTERFACE_ERROR.getMessage());

        return JSON.toJSONString(res);
    }

    /**
     * @author LiiiYiAn
     * @createDate 2023/9/25 10:32
     * @description 通用的系统错误方法
     * @return java.lang.String
     **/
    public static String sysError() {
        ApiResponse res = new ApiResponse<>(ApiResponseCodeEnum.SYSTEM_ERROR.getCode(), ApiResponseCodeEnum.SYSTEM_ERROR.getMessage());
        
        return JSON.toJSONString(res);
    }

}
